Providing a Flex Front End to Your Struts Applications
Table of Contents
Improving the View in Your MVC Architecture
Struts and Flex are both based on a Model-View-Controller (MVC) architecture.
- Struts primarily focuses on the controller part of the MVC architecture. In addition, it provides JSP tag libraries to help you create the view in a traditional JSP/HTML environment.
- Flex primarily focuses on the view part of the MVC architecture. It also provides some controller logic to help communicate with remote systems and implement security.
The promise of the Model-View-Controller architecture is that by creating components with a well-defined and limited scope in your application, you increase the reusability of these components and improve the maintainability of the overall system. In addition, you should also be able to swap components in one partition without affecting the entire system. The Flex/Struts integration builds on that promise. In many ways, using Flex in your Struts application simply consists of providing your application with an alternate, richer, and smarter view.
You can either entirely replace your existing views with a Flex-based user interface, or let the two view systems coexist in the same application, and even in the same page. For example, you can still provide browsing aspects of your site with HTML, while the Flex application provides the more interactive sections of the site.
Although you can consider the Flex application part of the View in a distributed MVC (Model 2 MVC), it also implements its own classic MVC architecture at the client side. A Flex application has its own view components (typically written in MXML), model components (representing data at the client side), and controller components (responsible for the communication with back-end systems, for example). This client-side separation of task is not available in HTML. Rich Internet Applications eliminate the need to rely heavily on complex Model 2 variations of MVC.
The Struts Framework
Struts primarily focuses on the Controller part of the MVC architecture and integrates with other technologies to provide the model and the view. For the model, Struts interacts with standard data access technologies, such as JDBC and EJB. For the view, Struts provides a set of tag libraries to help you create JavaServer Pages. Struts also works with other presentation technologies such as Velocity, XSLT, and, as this article describes, the Flex presentation server.
The following table describes the key components of the Struts framework.
| ActionServlet | ActionServlet is Struts' front controller servlet. In a Struts application, all HTTP requests route through the ActionServlet. |
|---|---|
| ActionMapping | ActionMapping objects are created from the struts-config.xml file. An ActionMapping object describes how to handle a specific request. It specifies:
|
| ActionForm | An ActionForm is a JavaBean that extends org.apache.struts.action.ActionForm.
An ActionForm object is automatically populated on the server side
with data entered from a form on the client side. |
| Action | An Action is defined by a class that extends org.apache.struts.action.Action. An Action object controls the action to perform for a specific request. The Action object typically interacts with Model objects (JavaBeans or EJBs) to execute some business logic. Based on the success or failure of the operation, the Action object uses an ActionForward object to tell the ActionServlet where to forward the request next. |
| ActionForward | Specifies a resource where the Action object should forward the request after handling it. |
Struts Workflow
The following diagram represents the workflow of a traditional Struts application:
Figure 1. The workflow of a traditional Struts application.
- The ActionServlet receives the request.
- Based on the request URI, the ActionServlet retrieves the ActionMapping that defines the request.
- The ActionServlet populates the ActionForm object from the HTTP
request. If the ActionMapping specifies that validation is required,
the ActionServlet calls the
validate()method on the ActionForm object. - The ActionServlet invokes the
execute()method of the Action object, providing the ActionForm object as a parameter. The method returns an ActionForward object specifying where to forward the request. - The ActionServlet forwards the request to the resource specified in the ActionForward object. This is typically a JSP (the view), but it can send the request to another Action.
- It sends the response back to the browser.